package com.yixun.controller;

import com.yixun.entity.YxUser;
import com.yixun.service.UserService;
import com.yixun.util.ParameterMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.Inet4Address;
import java.util.Map;

public class BaseController {

    protected static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
    protected int pageNo;
    protected int pageSize;

    @Autowired
    private UserService userService;

    /**
     * 获取当前用户
     */
    public YxUser getSessionUser(){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        return (YxUser) session.get("data");
    }

    public BaseController(){
        pageNo = 1;
        pageSize = 10;
    }

    /**
     * springMVC 获取request
     */
    public HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取response
     */
    public HttpServletResponse getResponse(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获取session
     */
    public HttpSession getSession() {
        return this.getRequest().getSession();
    }

    /**
     * 获取ServletContext
     */
    public ServletContext getServletContext(){
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        return webApplicationContext.getServletContext();
    }

    /**
     * 获取ModelAndView
     */
    public ModelAndView getModelAndView(){
        return new ModelAndView();
    }

    public ModelAndView get404ModelAndView() {
        ModelAndView view = new ModelAndView();
        view.setViewName("404");
        return view;
    }

    /**
     * 过滤参数
     */
    public ParameterMap getParameterMap(){
        ParameterMap pm = new ParameterMap(this.getRequest());
        pm.put("rip",getRemortIP());
        return pm;
    }

    /**
     * 获取ip
     */
    public String getRemortIP(){
        HttpServletRequest request = this.getRequest();
        String ip;
        if (null == request.getHeader("x-forwarded-for")) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        return ip;
    }

    /**
     * 获取port
     */
    public int getPort(){
        return this.getRequest().getServerPort();
    }

    /**
     * 获取ip
     */
    public String getIpAddr(){
        HttpServletRequest request = this.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public String getIpv4(){
        try {
            return Inet4Address.getLocalHost().getHostAddress();
        } catch (Exception e) {
            return "localhost";
        }
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * Get cookie value by cookie name.
     */
    public String getCookie(String name) {
        return getCookie(name, null);
    }

    /**
     * Get cookie value by cookie name.
     */
    public String getCookie(String name, String defaultValue) {
        Cookie cookie = getCookieObject(name);
        return cookie != null ? cookie.getValue() : defaultValue;
    }

    /**
     * Get cookie object by cookie name.
     */
    public Cookie getCookieObject(String name) {
        Cookie[] cookies = getRequest().getCookies();
        if (cookies != null)
            for (Cookie cookie : cookies)
                if (cookie.getName().equals(name))
                    return cookie;
        return null;
    }

}
